package com.ydtg.servelt;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ydtg.bean.Account;
import com.ydtg.bean.Materiel;
import com.ydtg.bean.Order;
import com.ydtg.bean.Picture;
import com.ydtg.dao.AccountDao;
import com.ydtg.dao.OrderDao;
import com.ydtg.dao.impl.AccountDaoImpl;
import com.ydtg.dao.impl.OrderDaoImpl;
import com.ydtg.service.MaterielService;
import com.ydtg.service.impl.MaterielServiceImpl;
import com.ydtg.service.impl.PictureServiceImpl;

/**
 * Servlet implementation class PictureServelt
 */
@WebServlet("/PictureServelt")
public class PictureServelt extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PictureServelt() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String res = "";
		String action = request.getParameter("action");

		if ("".equals(action)) {
			PictureServiceImpl psi=new PictureServiceImpl();
			MaterielService ms = new MaterielServiceImpl();
			Materiel m = new Materiel();
			Picture pic=new Picture();
			List<Materiel> materiels = ms.getall(null);
			int shuliang = ms.shuliang();
			List<Picture>imgList=psi.getall(pic);
			Map<String, Object> map = new HashMap<String, Object>();
			if (materiels!=null&&imgList!=null) {
				map.put("code", 0);
				map.put("msg", "成功");
				map.put("shuliang", shuliang);
				map.put("materiels", materiels);
				map.put("imgList", imgList);
			}else {
				map.put("code", -1);
				map.put("msg", "失败");
			}
			res = JSON.toJSONString(map);

			//ajax返回数据，不返回页面
			PrintWriter pw = response.getWriter();
			pw.write(res);
			pw.flush();
			pw.close();
		}


		if ("dl".equals(action)) {
			String yonghu = request.getParameter("yonghu");
			String pwd = request.getParameter("pwd");

			Account acc = new Account();
			AccountDao ad= new AccountDaoImpl();
			acc.setUser(yonghu);
			acc.setPwd(pwd);
			List<Account> list = ad.getTypefaceList(acc);

			Map<String, Object> map = new HashMap<String, Object>();
			if (list!=null&&list.size()>0) {
				Account acc1 =  list.get(0);
				if (acc1.getStaff()==0) {
					map.put("code", 0);
					map.put("msg", "登录成功");
					map.put("list", list);
				}

			}else {
				map.put("code", -1);
				map.put("msg", "登录失败");
			}
			res = JSON.toJSONString(map);

			//ajax返回数据，不返回页面
			PrintWriter pw = response.getWriter();
			pw.write(res);
			pw.flush();
			pw.close();

		}
		if ("gm".equals(action)) {
			String name = request.getParameter("name");
			int jiage = Integer.parseInt(request.getParameter("jiage"));
			String yh = request.getParameter("yh");
			int sl = Integer.parseInt(request.getParameter("sl"));

			Order od = new Order();
			od.setMateriel(name);
			od.setNumber(sl);
			od.setPrice(jiage);
			od.setCustomer(yh);
			od.setTotalprice(jiage*sl);
			OrderDao oDao = new OrderDaoImpl();
			int i =  oDao.add(od);

			Map<String, Object> map = new HashMap<String, Object>();
			if (i>0) {;
				map.put("code", 0);
				map.put("msg", "购买成功");
			}else {
				map.put("code", -1);
				map.put("msg", "购买失败");
			}
			res = JSON.toJSONString(map);

			//ajax返回数据，不返回页面
			PrintWriter pw = response.getWriter();
			pw.write(res);
			pw.flush();
			pw.close();

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
